package com.code.family.config.exception;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.code.family.utils.ResultVo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * @author 上玄
 */
@RestControllerAdvice
@ControllerAdvice(basePackages = "com.code.family")
public class GlobalExceptionHandler {

    private static final Log log = LogFactory.get();

    // 处理权限不足，UnauthorizedException
    @ExceptionHandler(UnauthorizedException.class)
    public ResultVo handleUnauthorizedException(UnauthorizedException ex) {
        log.info("权限不足");
        return ResultVo.error("用户没有权限访问");
    }
    // 自定义异常处理@ExceptionHandler,主要用于CustomException
    @ExceptionHandler(CustomException.class)
    public ResultVo customError(CustomException e) {
        return ResultVo.error("自定义异常");
    }

    // 统一异常处理@ExceptionHandler,主要用于Exception
    @ExceptionHandler(Exception.class)
    public ResultVo error(Exception e) {
        log.error("异常信息：", e);
        return ResultVo.error("系统异常，请联系管理员");
    }

}
